package com.times.util;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.times.exception.SystemException;

/**
 * ClassName: PropUtils <br/>
 * Function: properties文件工具类
 * @author 
 * @version 2011年11月12日
 */
public class PropUtils {
    
    private static final Logger logger = Logger.getLogger(PropUtils.class);

    public static Properties getProperties(String path) {
        Properties props = new Properties();
        try {
            InputStream is = PropUtils.class.getResourceAsStream(path);
            props.load(is);
        } catch (Exception e) {
            logger.error("error read property files, the file is " + path);
            throw new SystemException(e.getMessage());
        }
        return props;
    }

    /**
     * @description: 将utf-8格式properties文件内容转码
     * @author 
     * @param value
     * @return
     */
    public static String transPropertyEncoding(String value) {
        try {
            if (value.equals(new String(value.getBytes("ISO-8859-1"), "ISO-8859-1"))) {
                value = new String(value.getBytes("ISO-8859-1"), "UTF-8");// 兼容utf-8格式的properties文件
            }
            return value;
        } catch (UnsupportedEncodingException e) {
            logger.error("could not translate property file encoding");
            throw new SystemException(e.getMessage());
        }
    }

}
